package cn.pegasus.teamwork.pegasus.service.impl;

import java.util.Date;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.iyizhan.teamwork.base.service.impl.BaseServiceImpl;
import cn.pegasus.teamwork.pegasus.entity.BussWasherDeposit;
import cn.pegasus.teamwork.pegasus.entity.BussWasherFunddetails;
import cn.pegasus.teamwork.pegasus.entity.BussWasherFundstatistics;
import cn.pegasus.teamwork.pegasus.entity.BussWasherInfo;
import cn.pegasus.teamwork.pegasus.mapper.BussWasherDepositMapper;
import cn.pegasus.teamwork.pegasus.mapper.BussWasherFunddetailsMapper;
import cn.pegasus.teamwork.pegasus.mapper.BussWasherFundstatisticsMapper;
import cn.pegasus.teamwork.pegasus.mapper.BussWasherInfoMapper;
import cn.pegasus.teamwork.pegasus.service.BussWasherDepositService;
import cn.pegasus.teamwork.util.DateCompareUtil;
import cn.pegasus.teamwork.util.SnowflakeIdWorker;
import cn.pegasus.teamwork.vo.WXOrderVO;
import cn.pegasus.teamwork.weixinpay.Constant;
import cn.pegasus.teamwork.weixinpay.service.WasherPayService;

/**
 * @author ryan.bian
 * @version
 */
@Service("bussWasherDepositService")
public class BussWasherDepositServiceImpl extends BaseServiceImpl<BussWasherDeposit, BussWasherDepositMapper>
		implements BussWasherDepositService {
	// 注入当前dao对象
	@Autowired
	private BussWasherDepositMapper washerDepositMapper;
	
	@Autowired
	private BussWasherFunddetailsMapper washerFunddetailsMapper;
	
	@Autowired
	private BussWasherFundstatisticsMapper washerFundstatisticsMapper;
	
	@Autowired
	private BussWasherInfoMapper washerInfoMapper;
	
	@Autowired
	private WasherPayService washerPayService;

	public BussWasherDepositServiceImpl() {
		setMapperClass(BussWasherDepositMapper.class, BussWasherDeposit.class);
	}

	@Override
	public Map<String, Object> createWasherDepositOrder(Map<String, Object> map, int retype, int orderType) {
		// 查询充值列表信息
		int orderBalance = 0;
		if (map.get("orderBalance") != null) {
			orderBalance = Integer.valueOf(map.get("orderBalance").toString());
		}
		// 创建本地订单
		String orderNo = createWasherDepositOrderVo(map, retype, orderType);
		if (!StringUtils.isEmpty(orderNo)) {
			// 用户同意授权，获得的code
			String code = map.get("openid").toString();

			// 调用统一下单service
			WXOrderVO VO = new WXOrderVO();
			VO.setBody("凤凰洗车-洗车工缴纳押金");
			VO.setOpenid(code);
			VO.setOrderNo(orderNo);
			VO.setNotityUrl(Constant.DTNOTIFY_URL);
			VO.setOrderBalance(orderBalance);
			Map<String, Object> signMap = washerPayService.unifiedOrderForWasher(VO);

			if (signMap != null) {
					return signMap;
			}else{
				return null;
			}
		} else {
			return null;
		}
	}

	/**
	 * 添加用户消费订单表信息
	 * 
	 * @param orderVo
	 */
	public String createWasherDepositOrderVo(Map<String, Object> map, int retype, int orderType) {
		try {
			String orderNo = String.valueOf(new SnowflakeIdWorker().nextId());
			BussWasherDeposit buc = new BussWasherDeposit();

			buc.setWasherId(Integer.valueOf(map.get("washerId").toString()));
			buc.setOrderNo(String.valueOf(new SnowflakeIdWorker().nextId()));
			// 订单状态（1-充值；2-购买套餐；3-提现）
			buc.setOrderType(orderType);
			if (map.get("desc") != null) {
				buc.setOrderDesc(map.get("desc").toString());
			}

			buc.setServicesNo(""); // 服务商返回订单
			// 的ID（项目或者套餐）
			buc.setOrderBalance(Long.valueOf(map.get("orderBalance").toString())); // 订单金额

			buc.setRechargeType(retype); // 充值方式（1-微信支付；2-余额支付）
			buc.setTradeType(1); // 交易类型(1-公众号 2-APP;)',
			buc.setOrderStatus(1);// 订单状态（1-未支付；2-已支付；3-失败；）',
			buc.setCreateTime(DateCompareUtil.getNowDate());// 订单创建时间',
			buc.setCreateUserId(Long.valueOf(map.get("washerId").toString()));// 创建用户id',

			washerDepositMapper.insertByEntity(buc);
			return orderNo;

		} catch (Exception e) {
			e.printStackTrace();
		}

		return null;
	}

	/**
	 * 洗车工支付押金后业务处理
	 */
	public boolean modifyWasherDepositOrder(String transaction_id, String orderNo) {
		BussWasherDeposit bwd = new BussWasherDeposit();
		bwd.setOrderNo(orderNo);
		bwd  = washerDepositMapper.selectSingle(bwd);
		if(bwd != null){
			// 创建洗车工资金明细
			createWasherFunddetailsVo(bwd);
			
			// 更新洗车工总资金新
			createWasherFundstatisticsVo(bwd);
			
			// 修改洗车工基本信息
			updateWasherInfoVo(bwd);
			
			return true;
		}
		return false;
	}

	/**
	 * 添加洗车工资金明细表
	 * 
	 * @param orderVo
	 */
	public String createWasherFunddetailsVo(BussWasherDeposit bwd) {
		try {
			String orderNo = String.valueOf(new SnowflakeIdWorker().nextId());
			BussWasherFunddetails buc = new BussWasherFunddetails();
			buc.setWasherId(bwd.getWasherId());
			buc.setOrderId(Long.valueOf(bwd.getId()));
			buc.setCapitalType(21);
			buc.setTransactionTime(new Date());
			buc.setPayBalance(bwd.getOrderBalance());
			buc.setGiveBalance(0l);
			buc.setCapitalBalance(0l);
			// 审核状态【提现审核】（0-不审核；1-审核中；2-审核通过；3-不通过；）
			buc.setExamineStatus(1);
			buc.setStatus(1);
			// 是否已打款（0- 不审核；1-待审核；2-待打款；3-已打款）
			buc.setIspaymoney(1);
			buc.setCreateTime(new Date());

			washerFunddetailsMapper.insertByEntity(buc);
			return orderNo;

		} catch (Exception e) {
			e.printStackTrace();
		}

		return null;
	}
	
	/**
	 * 添加洗车工资金明细表
	 * 
	 * @param orderVo
	 */
	public void createWasherFundstatisticsVo(BussWasherDeposit bwd) {
		try {
			BussWasherFundstatistics buc = new BussWasherFundstatistics();
			buc.setWasherId(Long.valueOf(bwd.getWasherId()));
			buc = washerFundstatisticsMapper.selectSingle(buc);
			if(buc != null){
				buc.setWasherId(Long.valueOf(bwd.getWasherId()));
				buc.setWasherDeposit(bwd.getOrderBalance());
				buc.setUpdateTime(new Date());

				washerFundstatisticsMapper.updateByEntity(buc);
			}else {				
				buc.setWasherId(Long.valueOf(bwd.getWasherId()));
				buc.setWasherDeposit(bwd.getOrderBalance());
				buc.setUpdateTime(new Date());

				washerFundstatisticsMapper.insertByEntity(buc);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 添加洗车工资金明细表
	 * 
	 * @param orderVo
	 */
	public void updateWasherInfoVo(BussWasherDeposit bwd) {
		try {
			BussWasherInfo buc = new BussWasherInfo();
			buc.setId(bwd.getWasherId());
			buc = washerInfoMapper.selectSingle(buc);
			if(buc != null){
				// 是否已交押金（ 1-是 2-否； )
				buc.setWasherIsdeposit(1);
				// 是否交押金审核通过-洗车工（ 1-通过 2-审核中；3-不通过 )
				buc.setWasherIsdepEntrust(2);;
				buc.setUpdateTime(new Date());

				washerInfoMapper.updateByEntity(buc);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
